package com.weimob.mcloud.wechatwork.customer.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.weimob.mcloud.wechatwork.core.base.SingleResponse;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.CustomerTag;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.TagDetail;
import com.weimob.mcloud.wechatwork.core.service.tag.CustomerTagCoreService;
import com.weimob.mcloud.wechatwork.core.service.tag.request.SingleCustomerTagRequest;
import com.weimob.mcloud.wechatwork.customer.service.CustomerEmployeeTagService;
import com.weimob.soa.common.response.SoaResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <P>
 *     企微官方客户&员工&标签关系相关
 * </P>
 * @author lvjunlong
 * @data 2023-01-15
 */
@Service
@Slf4j
public class CustomerEmployeeTagServiceImpl implements CustomerEmployeeTagService {

    @Reference
    private CustomerTagCoreService customerTagCoreService;

    /**
     * 添加员工&客户&标签
     *
     * @param privateSphere 企微唯一标识
     * @param externalUserId 腾讯客户id
     * @param orgUserId 企微助手员工id
     * @param tagIds 企微官方标签ids
     * @return
     */
    @Override
    public Boolean saveCustomerEmployeeTag(PrivateSphere privateSphere, String externalUserId, String orgUserId, List<String> tagIds) {
        if (Objects.isNull(privateSphere) || StringUtils.isBlank(externalUserId) || StringUtils.isBlank(orgUserId)) {
            return false;
        }

        SingleCustomerTagRequest<TagDetail> request = new SingleCustomerTagRequest<>();
        request.setExternalUserId(externalUserId);
        request.setOrgUserId(orgUserId);
        request.setTagIdList(tagIds);
        request.setPrivateSphere(privateSphere);

        log.info("添加员工&客户&标签关系, privateSphere:{}, externalUserId:{}, orgUserId:{}", JSON.toJSONString(privateSphere), externalUserId, orgUserId);
        SoaResponse<SingleResponse<Boolean>, Void> soaResponse = customerTagCoreService.saveCustomerEmployeeTag(request);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("添加员工&客户&标签关系失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
            return false;
        }

        SingleResponse<Boolean> responseVo = soaResponse.getResponseVo();
        if (null == responseVo || null == responseVo.getData()) {
            return false;
        }

        return responseVo.getData();
    }

    /**
     * 编辑员工&客户&标签
     *
     * @param privateSphere 企微唯一标识
     * @param externalUserId 腾讯客户id
     * @param orgUserId 企微助手员工id
     * @param tagIds 企微官方标签ids
     * @return
     */
    @Override
    public Boolean updateCustomerEmployeeTag(PrivateSphere privateSphere, String externalUserId, String orgUserId, List<String> tagIds) {
        if (Objects.isNull(privateSphere) || StringUtils.isBlank(externalUserId) || StringUtils.isBlank(orgUserId)) {
            return false;
        }

        SingleCustomerTagRequest<TagDetail> request = new SingleCustomerTagRequest<>();
        request.setExternalUserId(externalUserId);
        request.setOrgUserId(orgUserId);
        request.setTagIdList(tagIds);
        request.setPrivateSphere(privateSphere);

        log.info("编辑员工&客户&标签关系, privateSphere:{}, externalUserId:{}, orgUserId:{}", JSON.toJSONString(privateSphere), externalUserId, orgUserId);
        SoaResponse<SingleResponse<Boolean>, Void> soaResponse = customerTagCoreService.updateCustomerEmployeeTag(request);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("编辑员工&客户&标签关系失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
            return false;
        }

        SingleResponse<Boolean> responseVo = soaResponse.getResponseVo();
        if (null == responseVo || null == responseVo.getData()) {
            return false;
        }

        return responseVo.getData();
    }

    /**
     * 查询员工&客户&标签
     * externalUserId必传，如果传了orgUserId，则根据externalUserId、orgUserId查询
     *
     * @param privateSphere 企微唯一标识
     * @param externalUserId 腾讯客户id
     * @param orgUserId 企微助手员工id
     * @param needFullInfo 是否补全标签信息
     * @return
     */
    @Override
    public List<TagDetail> fetchCustomerEmployeeTag(PrivateSphere privateSphere, String externalUserId, String orgUserId, Boolean needFullInfo) {
        if (Objects.isNull(privateSphere) || StringUtils.isBlank(externalUserId)) {
            return new ArrayList<>();
        }

        SingleCustomerTagRequest<TagDetail> request = new SingleCustomerTagRequest<>();
        request.setPrivateSphere(privateSphere);
        request.setExternalUserId(externalUserId);
        request.setOrgUserId(orgUserId);
        request.setNeedFullInfo(needFullInfo);

        SoaResponse<SingleResponse<List<TagDetail>>, Void> soaResponse = customerTagCoreService.fetchCustomerEmployeeTag(request);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("查询员工&客户&标签关系失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
            return new ArrayList<>();
        }

        SingleResponse<List<TagDetail>> responseVo = soaResponse.getResponseVo();
        if (null == responseVo || CollectionUtils.isEmpty(responseVo.getData())) {
            return new ArrayList<>();
        }
        return responseVo.getData();
    }

    /**
     * 添加客户&标签,没有员工
     *
     * @param privateSphere 企微唯一标识
     * @param externalUserId 腾讯客户id
     * @param tagIds 企微官方标签ids
     * @return
     */
    @Override
    public Boolean saveCustomerTag(PrivateSphere privateSphere, String externalUserId, List<String> tagIds) {
        if (Objects.isNull(privateSphere) || StringUtils.isBlank(externalUserId)) {
            return false;
        }

        SingleCustomerTagRequest<TagDetail> request = new SingleCustomerTagRequest<>();
        request.setExternalUserId(externalUserId);
        request.setTagIdList(tagIds);
        request.setPrivateSphere(privateSphere);

        SoaResponse<SingleResponse<Boolean>, Void> soaResponse = customerTagCoreService.saveCustomerTag(request);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("添加客户&标签关系失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
            return false;
        }

        SingleResponse<Boolean> responseVo = soaResponse.getResponseVo();
        if (null == responseVo || null == responseVo.getData()) {
            return false;
        }

        return responseVo.getData();
    }

    /**
     * 编辑客户&标签,没有员工
     *
     * @param privateSphere 企微唯一标识
     * @param externalUserId 腾讯客户id
     * @param tagIds 企微官方标签ids
     * @return
     */
    @Override
    public Boolean updateCustomerTag(PrivateSphere privateSphere, String externalUserId, List<String> tagIds) {
        if (Objects.isNull(privateSphere) || StringUtils.isBlank(externalUserId)) {
            return false;
        }

        SingleCustomerTagRequest<TagDetail> request = new SingleCustomerTagRequest<>();
        request.setExternalUserId(externalUserId);
        request.setTagIdList(tagIds);
        request.setPrivateSphere(privateSphere);

        SoaResponse<SingleResponse<Boolean>, Void> soaResponse = customerTagCoreService.updateCustomerTag(request);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("编辑客户&标签关系失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
            return false;
        }

        SingleResponse<Boolean> responseVo = soaResponse.getResponseVo();
        if (null == responseVo || null == responseVo.getData()) {
            return false;
        }

        return responseVo.getData();
    }

    @Override
    public CustomerTag getCustomerTag(PrivateSphere privateSphere, String externalUserId) {

        if (Objects.isNull(privateSphere) || StringUtils.isBlank(externalUserId)) {
            return null;
        }

        SingleCustomerTagRequest<CustomerTag> request = new SingleCustomerTagRequest<>();
        request.setPrivateSphere(privateSphere);
        request.setExternalUserId(externalUserId);

        SoaResponse<SingleResponse<CustomerTag>, Void> soaResponse = customerTagCoreService.getCustomerTag(request);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("查询客户标签关系失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
            return null;
        }

        SingleResponse<CustomerTag> responseVo = soaResponse.getResponseVo();
        if (null == responseVo || null == responseVo.getData()) {
            return null;
        }

        return responseVo.getData();
    }
}
